package com.xn.app.muying.adpter;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xn.app.muying.R;
import com.xn.app.muying.bean.BabyStudyBean;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/10/10.
 */
public class BabyStudyAdpter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private ArrayList<BabyStudyBean> data = new ArrayList();
    private Handler handler = new Handler();
    private ImageLoader imageLoader = null;
    private DisplayImageOptions options = null;


    public BabyStudyAdpter(Context context, ImageLoader loader,
                           DisplayImageOptions options) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.imageLoader = loader;
        this.options = options;
    }

    public void getData(ArrayList<BabyStudyBean> data) {
        this.data=data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ViewHolder holder = new ViewHolder();
        if (view == null) {
            view = inflater.inflate(R.layout.education_fragment_babystudy_item, null);
            holder.iv_picture = (ImageView) view.findViewById(R.id.education_fragment_babystudy_iv);
            holder.tv_name = (TextView) view.findViewById(R.id.education_fragment_babystudy_tv);
            holder.education_fragment_babystudy_time= (TextView) view.findViewById(R.id.education_fragment_babystudy_time);
            holder.education_fragment_babystudy_data= (TextView) view.findViewById(R.id.education_fragment_babystudy_data);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.tv_name.setText(data.get(position).getTitle());
        holder.iv_picture.setTag(data.get(position).getPictureUrl());
        //设置图片，如果内存中有，优先从内存中读取
        if (data.get(position).getPictureUrl()!= null && !data.get(position).getPictureUrl().equals("null")) {
            imageLoader.displayImage(data.get(position).getPictureUrl(), holder.iv_picture, options);
        }
        holder.education_fragment_babystudy_time.setText(data.get(position).getCreationTime());
        holder.education_fragment_babystudy_data.setText("阅读详情>");
        return view;
    }

    class ViewHolder {
        ImageView iv_picture;
        TextView tv_name;
        TextView education_fragment_babystudy_time;
        TextView education_fragment_babystudy_data;
    }
}
